package com.easylinkin.linkappapi.webcammanage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.easylinkin.linkappapi.webcammanage.entity.DeviceAttributeStatus;
import com.easylinkin.linkappapi.webcammanage.entity.EnterpriseVideoTreeDTO;
import com.easylinkin.linkappapi.webcammanage.entity.EnterpriseVideoTreeQueryDTO;
import com.easylinkin.linkappapi.webcammanage.service.DeviceAreaHandlerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * class info : 岳阳石化--摄像头--设备管理
 *
 * @author 刘启航
 * @date 2021/4/20 10:30
 */
@RestController
@RequestMapping("/dahc")
@Slf4j
public class DeviceAreaHandlerController {

    @Autowired
    private DeviceAreaHandlerService deviceAreaHandlerService;
    @Resource
    private LinkappUserContextProducer linkappUserContextProducer;

    @PostMapping("getDeviceVideoList")
    public RestMessage getDeviceVideoList(@RequestBody RequestModel<Device> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        requestModel.getCustomQueryParams().setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        IPage<DeviceAttributeStatus> deviceVideoList = deviceAreaHandlerService.getDeviceVideoList(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(deviceVideoList).build();
    }

    @PostMapping("getEnterpriseVideoList")
    public RestMessage getEnterpriseVideoList(@RequestBody RequestModel<Device> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<DeviceAttributeStatus> deviceVideoList = deviceAreaHandlerService.getEnterpriseVideoList(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(deviceVideoList).build();
    }

    @PostMapping("getEnterpriseVideoTree")
    public RestMessage getEnterpriseVideoTree(@RequestBody EnterpriseVideoTreeQueryDTO enterpriseVideoTreeQueryDTO) {
        List<Map> list = deviceAreaHandlerService.getEnterpriseVideoTree(enterpriseVideoTreeQueryDTO);
        return RestBuilders.successBuilder().data(list).build();
    }

    @PostMapping("getDeviceVideoCount")
    public RestMessage getDeviceVideoCount(@RequestBody Device device) {
        Integer deviceVideoCount = deviceAreaHandlerService.getDeviceVideoCount(device);
        return RestBuilders.successBuilder().data(deviceVideoCount).build();
    }

    @GetMapping("webcamJumpPage")
    public void webcamJumpPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String sessionId = request.getParameter("sessionId");
        String redirectUrl = request.getParameter("redirectUrl");
        log.info("sessionId=" + sessionId + ", redirectUrl=" + redirectUrl);

        StringBuilder s = new StringBuilder();
        s.append("<!DOCTYPE html>")
                .append("<html lang=\"zh-CN\">")
                .append("<head>")
                .append("<script>")
                .append("sessionStorage.setItem('isLogin', '1');")
                .append("document.cookie='JSESSIONID=" + sessionId + "';")
//                .append("location.href = location.origin'/#/fingerprint'")//测试使用, 需要修改
                .append("location.href = `${location.origin}/`" + redirectUrl)
                .append("</script>")
                .append("</head>")
                .append("</html>");

        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setContentType(MediaType.TEXT_HTML_VALUE);
        response.getWriter().write(s.toString());
    }

}
